/**
 * 
 */
package com.gowants.pems.service;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.gowants.pems.mapper.SysRoleMapper;
import com.gowants.pems.model.SysPower;
import com.gowants.pems.model.SysRole;
import com.gowants.pems.service.SysPowerService;
import com.gowants.pems.util.DateUtil;
import com.gowants.pems.util.JSONList;
import com.gowants.pems.util.JSONMap;

/**
 * @author 86150
 *
 */
@Service(value="scmsRoleService")
public class SysRoleService extends NewBaseService<SysRole>
{
    
    @Autowired
    private SysRoleMapper scmsRoleMapper;
    @Autowired
	private SysPowerService scmsPowerService;
    @Override
    public int save(SysRole record)
    {
        return scmsRoleMapper.save(record);
    }

    @Override
    public int delete(String primaryKey)
    {
        return scmsRoleMapper.delete(primaryKey);
    }
    @Transactional(rollbackFor = Exception.class)
    public int deleteAll(String primaryKey)
    {
    	int i=0;
    
    	try
    	{
    		i= scmsRoleMapper.delete(primaryKey);
    		if(i>0)
    		{
    			scmsPowerService.deleteByRoleId(primaryKey);
    		}
    	} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			// throw e;
		}
    	return i;
    }
    @Override
    public int update(SysRole record)
    {
        return scmsRoleMapper.update(record);
    }

    @Override
    public SysRole selectOne(String primaryKey)
    {
        return scmsRoleMapper.selectOne(primaryKey);
    }

    @Override
    public List<SysRole> select(SysRole record)
    {
        return scmsRoleMapper.select(record);
    }

    @Override
    public int getCount(SysRole record)
    {
        // TODO Auto-generated method stub
        return 0;
    }
    @Transactional(rollbackFor = Exception.class)
    public String saveNew(SysRole record, String userId, JSONList scmsPowerList) 
    {
    	String code="TSMS_0000036";
    	try
    	{
	        //校验角色名是否重复
	        Integer saveCheckNum = scmsRoleMapper.saveCheckNameRepeat(record);
	        if(saveCheckNum > 0) 
	        {
	            return "TSMS_0000035";//重复添加
	        }
	        record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
	        record.setCreateUid(userId);
	        record.setCreateTime(DateUtil.getDate());
	        Integer saveNum = scmsRoleMapper.save(record);
	        if(saveNum <= 0) 
	        {
	            return "TSMS_0000036";//添加失败
	        }
	        
	        SysPower fp = new SysPower();
			fp.setRoleId(record.getId());
			if (null != scmsPowerList && scmsPowerList.size()>0) {
				for (Object obj : scmsPowerList) {
					JSONMap map = JSONMap.parseJSONMap(obj.toString());
					fp.setPermissionId(map.getString("id"));
					fp.setId(UUID.randomUUID().toString());
					fp.setCreateTime(DateUtil.getDate());
					fp.setCreateUid(record.getCreateUid());
					scmsPowerService.save(fp);
				}
			}
	        return "TSMS_0000001";//添加成功
    	} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			// throw e;
		}
    	return code;
    }
    @Transactional(rollbackFor = Exception.class)
    public String updateNew(SysRole record,String userId, JSONList scmsPowerList) 
    {
    	String code="TSMS_0000053";
    	try
    	{
	        //校验角色名是否重复
	        Integer updateCheckNum = scmsRoleMapper.updateCheckNameRepeat(record);
	        if(updateCheckNum > 0) 
	        {
	            return "TSMS_0000035";//重复添加
	        }
	        Integer updateNum = scmsRoleMapper.update(record);
	        if(updateNum <= 0) 
	        {
	            return "TSMS_0000053";//修改失败
	        }
	        scmsPowerService.deleteByRoleId(record.getId());
	        SysPower fp = new SysPower();
			fp.setRoleId(record.getId());
			if (null != scmsPowerList && scmsPowerList.size()>0) {
				for (Object obj : scmsPowerList) {
					JSONMap map = JSONMap.parseJSONMap(obj.toString());
					fp.setPermissionId(map.getString("id"));
					fp.setId(UUID.randomUUID().toString());
					fp.setCreateTime(DateUtil.getDate());
					fp.setCreateUid(userId);
					scmsPowerService.save(fp);
				}
			}
	        
	        return "TSMS_0000002";//修改成功
    	} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			// throw e;
		}
    	return code;
    }
    
}
